<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>标签过滤调试</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="assets/css/ilovefree-migration.css">
    <link rel="stylesheet" href="assets/css/enhanced-layout.css">
    <link rel="stylesheet" href="assets/css/fontawesome-icons.css">
    <style>
        .debug-container {
            padding: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }
        .debug-info {
            background: rgba(3, 150, 255, 0.1);
            border: 1px solid var(--ilovefree-blue);
            padding: 15px;
            border-radius: var(--radius-md);
            margin: 20px 0;
            font-family: monospace;
            font-size: 14px;
        }
        .nav-card-debug {
            border: 1px solid #ddd;
            margin: 10px 0;
            padding: 15px;
            border-radius: var(--radius-md);
        }
        .tag-debug {
            background: #f0f0f0;
            padding: 5px 10px;
            border-radius: 3px;
            font-family: monospace;
            font-size: 12px;
            margin-top: 5px;
        }
        .test-buttons {
            margin: 20px 0;
        }
        .test-btn {
            padding: 10px 20px;
            margin: 5px;
            background: var(--ilovefree-blue);
            color: white;
            border: none;
            border-radius: var(--radius-md);
            cursor: pointer;
        }
        .test-btn:hover {
            background: var(--ilovefree-accent);
        }
    </style>
</head>
<body>
    <div class="debug-container">
        <h1 style="text-align: center; color: var(--text-primary); margin-bottom: 40px;">
            🐛 标签过滤调试页面
        </h1>

        <?php
        require_once 'config/config.php';
        require_once 'includes/database.php';
        $db = new Database();

        // 获取分类2的数据
        $category = null;
        $categories = $db->getCategories();
        foreach ($categories as $cat) {
            if ($cat['id'] == 2) {
                $category = $cat;
                break;
            }
        }

        $navigations = $db->getNavigations(2);
        $tags = $db->getTags(2);
        ?>

        <div class="debug-info">
            <h3>📊 调试信息</h3>
            <p>分类: <?php echo htmlspecialchars($category['name']); ?> (ID: <?php echo $category['id']; ?>)</p>
            <p>导航总数: <?php echo count($navigations); ?></p>
            <p>标签总数: <?php echo count($tags); ?></p>
        

        <!-- 标签容器 -->
        <div class="category-header">
            <div class="category-title">
                <div class="category-icon">
                <h2 class="category-name"><?php echo htmlspecialchars($category['name']); ?></h2>
            
            <div class="tags-container" id="category-2">
                <!-- 推荐标签 -->
                <button class="tag-btn active" data-tag-id="0" data-tag-filter="推荐" onclick="testFilter(0)">
                    推荐
                    <span class="tag-count"><?php echo count($navigations); ?></span>
                </button>

                <?php
                // 过滤掉推荐标签
                $filteredTags = array_filter($tags, function($tag) {
                    return trim($tag['name_cn']) !== '推荐';
                });
                ?>
                <?php foreach ($filteredTags as $tag): ?>
                    <?php
                    // 计算该标签下的导航数量
                    $tagNavCount = 0;
                    foreach ($navigations as $nav) {
                        if (!empty($nav['tag_ids']) && is_array($nav['tag_ids'])) {
                            if (in_array($tag['id'], $nav['tag_ids'])) {
                                $tagNavCount++;
                            }
                        }
                    }
                    ?>
                    <button class="tag-btn" data-tag-id="<?php echo $tag['id']; ?>" data-tag-filter="<?php echo htmlspecialchars($tag['name_cn']); ?>" onclick="testFilter(<?php echo $tag['id']; ?>)">
                        <?php echo htmlspecialchars($tag['name_cn']); ?>
                        <span class="tag-count"><?php echo $tagNavCount; ?></span>
                    </button>
                <?php endforeach; ?>
            
        

        <div class="test-buttons">
            <button class="test-btn" onclick="showAllCards()">显示所有卡片</button>
            <button class="test-btn" onclick="debugTags()">调试标签数据</button>
            <button class="test-btn" onclick="debugCategories()">调试分类数据</button>
        

        <!-- 导航卡片 -->
        <div class="nav-grid" id="nav-grid-2">
            <?php foreach ($navigations as $nav): ?>
                <?php
                // 构建标签过滤器
                $tagFilters = [];
                if (!empty($nav['tag_ids']) && is_array($nav['tag_ids'])) {
                    foreach ($nav['tag_ids'] as $tagId) {
                        foreach ($tags as $tag) {
                            if ($tag['id'] == $tagId) {
                                $tagFilters[] = htmlspecialchars($tag['name_cn']);
                                break;
                            }
                        }
                    }
                    $tagFilters[] = '推荐';
                }
                $tagFiltersString = implode(',', array_unique($tagFilters));
                ?>
                <div class="nav-card-debug" data-tag-filters="<?php echo $tagFiltersString; ?>">
                    <h4><?php echo htmlspecialchars($nav['name']); ?></h4>
                    <p><?php echo htmlspecialchars($nav['description']); ?></p>
                    <div class="tag-debug">
                        <strong>Tag IDs:</strong> <?php echo json_encode($nav['tag_ids']); ?><br>
                        <strong>Tag Filters:</strong> <?php echo $tagFiltersString; ?>
                    
                
            <?php endforeach; ?>
        
    

    <script src="assets/js/php-ilovefree-migration.js"></script>
    <script>
        function testFilter(tagId) {
            console.log('🏷️ 测试标签过滤:', tagId);

            // 移除所有active类
            document.querySelectorAll('.tag-btn').forEach(btn => {
                btn.classList.remove('active');
            });

            // 添加active类到点击的按钮
            document.querySelector(`.tag-btn[data-tag-id="${tagId}"]`).classList.add('active');

            // 过滤卡片
            const cards = document.querySelectorAll('.nav-card-debug');
            let visibleCount = 0;

            cards.forEach(card => {
                const filters = card.getAttribute('data-tag-filters') || '';
                const tags = filters.split(',').map(tag => tag.trim()).filter(tag => tag);

                const shouldShow = tagId === 0 || tags.includes(getTagFilterName(tagId));

                if (shouldShow) {
                    card.style.display = 'block';
                    visibleCount++;
                } else {
                    card.style.display = 'none';
                }
            });

            console.log(`✅ 过滤完成，显示 ${visibleCount} 个卡片`);
        }

        function getTagFilterName(tagId) {
            if (tagId === 0) return '推荐';
            const btn = document.querySelector(`.tag-btn[data-tag-id="${tagId}"]`);
            return btn ? btn.getAttribute('data-tag-filter') : '';
        }

        function showAllCards() {
            document.querySelectorAll('.nav-card-debug').forEach(card => {
                card.style.display = 'block';
            });
            console.log('✅ 显示所有卡片');
        }

        function debugTags() {
            console.log('=== 调试标签数据 ===');
            document.querySelectorAll('.tag-btn').forEach(btn => {
                const tagId = btn.getAttribute('data-tag-id');
                const tagFilter = btn.getAttribute('data-tag-filter');
                console.log(`标签: ID=${tagId}, Filter="${tagFilter}"`);
            });
        }

        function debugCategories() {
            console.log('=== 调试分类数据 ===');
            if (window.phpPickfreeMigration) {
                console.log('Categories:', window.phpPickfreeMigration.categories);
                console.log('Selected Tags:', window.phpPickfreeMigration.selectedTags);
            } else {
                console.log('❌ 迁移系统未初始化');
            }
        }

        document.addEventListener('DOMContentLoaded', () => {
            console.log('🎯 调试页面已加载');
            console.log('📊 导航卡片总数:', document.querySelectorAll('.nav-card-debug').length);

            // 等待迁移系统初始化
            setTimeout(() => {
                console.log('📊 迁移系统状态:', window.phpPickfreeMigration ? '已初始化' : '未初始化');
                if (window.phpPickfreeMigration) {
                    console.log('📊 分类数据:', window.phpPickfreeMigration.categories);
                }
            }, 2000);
        });
    </script>
</body>
</html>